package com.ui.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

public class ImageUtil {

	public static Bitmap roundCorners(final Bitmap bitmap, final float radius) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(android.graphics.Color.WHITE);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		
		Bitmap clipped = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(clipped);
		canvas.drawRoundRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), radius, radius, paint);
		canvas.drawBitmap(bitmap, 0, 0, paint);
		bitmap.recycle();
		return clipped;
	}

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,final float radius) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0xff424242);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawRoundRect(rectF, radius, radius, paint);
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
	
	public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
		int w = bmp.getWidth();
		int h = bmp.getHeight();
		if(radius <= 0){
			radius = w > h ? h/2 : w/2; 
		}
		Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0xff000000);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawCircle(w/2, h/2, radius, paint);
		bmp.recycle();
		bmp = null;
		return bitmap;
    }  
}
